package com.jeeplus.modules.guide.web;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.guide.service.*;
import com.jeeplus.modules.guide.utils.AESEncryptionUtils;
import com.jeeplus.modules.sys.service.UserService;
import com.jeeplus.modules.sys.utils.EncryptUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Api(tags="税务统计分析")
@RestController
@RequestMapping("/sw")
@RequiredArgsConstructor
public class SWController {
    private static final Logger log = LoggerFactory.getLogger(SWController.class);
    private final SWService swService;
    private final MobileIndexService mobileIndexService;
    private final UserService userService;
    private final MobilePublicSecurityTwoService mobilePublicSecurityTwoService;
    //私钥
    @Value("${GX_PUBLIC_KEY}")
    private String GX_PUBLIC_KEY;
    //对接AppKeyValue
    @Value("${AppKeyValue}")
    private String AppKeyValue;


    /**
     * 一码办成事事项分类展示
     *
     * @return
     */
    @ApiOperation(value = "税务总办件量统计")
    @GetMapping(value ="toTotalSW")
    public AjaxJson toTotalSW(){
        return AjaxJson.success().put("data",swService.toTotalSW(false));
    }

    /**
     * 一码办成事事项分类展示
     *
     * @return
     */
    @ApiOperation(value = "税务办件趋势分析")
    @GetMapping(value ="toHourSW")
    public AjaxJson toHourSW(){
        return swService.toHourSW();
    }

    /**
     * 税务窗口平均办理时长top10
     *
     * @return
     */
    @ApiOperation(value = "税务窗口平均办理时长top10")
    @GetMapping(value ="toWinTopSW")
    public AjaxJson toWinTopSW(){
        return AjaxJson.success().put("data",swService.toWinTopSW());
    }

    /**
     * 今日业务办件分析
     *
     * @return
     */
    @ApiOperation(value = "今日业务办件分析")
    @GetMapping(value ="toDayTypeSW")
    public AjaxJson toDayTypeSW(){
        return AjaxJson.success().put("data",swService.toDayTypeSW());
    }

    /**
     * 总业务办件分析
     *
     * @return
     */
    @ApiOperation(value = "总业务办件分析")
    @GetMapping(value ="toAllTypeSW")
    public AjaxJson toAllTypeSW(){
        return swService.toAllTypeSW();
    }


    /**
     * 今日群众办件分析
     *
     * @return
     */
    @ApiOperation(value = "今日群众办件分析")
    @GetMapping(value ="toWaitPersonList")
    public AjaxJson toWaitPersonList(){
        return AjaxJson.success().put("data",swService.toWaitPersonList(false));
    }

    /**
     * 窗口办件状态分析
     * W开始办理 S结束办理 L空闲 N未登录 员工0000 未登录
     * @return
     */
    @ApiOperation(value = "窗口办件状态分析")
    @GetMapping(value ="todWinList")
    public AjaxJson todWinList(){
        return mobilePublicSecurityTwoService.findWinList();
    }


    /**
     * 通过用户id获取用户信息
     * getUserValueById
     * @return 受影响的行数
     */
    @GetMapping("/getUserValueById")
    public AjaxJson getUserValueById(@RequestHeader("AppKey") String AppKey, String id) {
        if (AppKeyValue.equals(AppKey)) {
            if (StringUtils.isBlank(id)) {
                return AjaxJson.error("参数为null!").put("code", 400);
            }
            try {
                return AjaxJson.success("get data success!").put("data", EncryptUtils.aesEncrypt(JSONObject.fromObject(userService.getUserValueById(id)).toString(), GX_PUBLIC_KEY));
            } catch (Exception e) {
                return AjaxJson.error("get data is error").put("code", 400);
            }
        } else {
            return AjaxJson.error("AppKey is error!").put("code", 400);
        }
    }

    /**
     * 推送税务群众办理进度
     * toUserValue
     * @return 受影响的行数
     */
    @GetMapping("/toUserValue")
    public AjaxJson toUserValue(@RequestHeader("AppKey") String AppKey, HttpServletRequest request) {
        if (AppKeyValue.equals(AppKey)) {
            try {
                String str, body = "";
                BufferedReader reader = request.getReader();
                while((str = reader.readLine()) != null){
                    body += str;
                }
                body = EncryptUtils.aesDecrypt(body, GX_PUBLIC_KEY);
                JSONObject json = JSONObject.fromObject(body);
                String endTime = null;
                if("1".equals(json.getString("matterCode")))endTime = json.getString("endTime");
                mobileIndexService.intoSWHistory(json.getString("matterName"),json.getString("matterCode"),json.getString("matterStatus"),json.getString("startTime"),endTime,json.getString("ids"),json.getString("gxUserId"));
                return AjaxJson.success("get data success!");
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxJson.error("get data is error"+e.getMessage()).put("code", 400);
            }
        } else {
            return AjaxJson.error("AppKey is error!").put("code", 400);
        }
    }



}
